package com.css.fxfzfxqh.modules.seismicHazardData.utils;

import com.css.fxfzfxqh.util.PlatformObjectUtils;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.util.GeometricShapeFactory;

public class EarthquakeImpactFieldUtils {

    /**
     * 定义地球半径（米）
     */
    //private final double R_EARTH = 6371000;
    private static final double R_EARTH = 6378137;

    /**
     * 定义地球赤道周长（米）
     */
    private static final double P_EARTH = 2 * Math.PI * R_EARTH;

    public static String  createInfluenceEllipse(String lon, String lat, String long_x, String short_y, String angle, String min_long_x, String min_short_y) {
        String result = "";
        try {
            if (!PlatformObjectUtils.isEmpty(lon) && !PlatformObjectUtils.isEmpty(lat)
                    && !PlatformObjectUtils.isEmpty(long_x) && !PlatformObjectUtils.isEmpty(short_y)) {
                double x = Double.parseDouble(lon);//经度
                double y = Double.parseDouble(lat);//纬度
                double macroaxis = Double.parseDouble(long_x) * 1000;//长轴（米）
                double brachyaxis = Double.parseDouble(short_y) * 1000;//短轴（米）
                double direction = 0;//旋转角度
                if (!PlatformObjectUtils.isEmpty(angle)) {
                    direction = Double.parseDouble(angle);//旋转角度
                }
                //创建椭圆
                Polygon ellipse = createEllipse(x, y, macroaxis, brachyaxis, direction);
                if (!PlatformObjectUtils.isEmpty(min_long_x) && !PlatformObjectUtils.isEmpty(min_short_y)) {//内圈椭圆
                    double min_macroaxis = Double.parseDouble(min_long_x) * 1000;//内圈长轴（米）
                    double min_brachyaxis = Double.parseDouble(min_short_y) * 1000;//内圈短轴（米）
                    //创建内圈椭圆
                    Polygon innerEllipse = createEllipse(x, y, min_macroaxis, min_brachyaxis, direction);
                    result = ellipse.toString() + ";" + innerEllipse.toString();
                    result = result.replace("));POLYGON ((", "), (");
                } else {//无内圈椭圆
                    //返回结果
                    result = ellipse.toString();
                }
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }

        return result;
    }

    /**
     * 根据中心点经纬度、长轴、短轴、角度生成椭圆
     *
     * @param x
     * @param y
     * @param macroaxis
     * @param brachyaxis
     * @param direction
     * @return
     */
    public static Polygon createEllipse(double x, double y, double macroaxis, double brachyaxis, double direction) {
        //将长短轴转换为度数
        double macroaxisDegree = parseXLengthToDegree(y, macroaxis);
        double brachyaxisDegree = parseYLengthToDegree(brachyaxis);
        //将夹角转换为弧度
        double radians = Math.toRadians(direction);
        //生成工厂类
        GeometricShapeFactory shapeFactory = new GeometricShapeFactory();
        //设置中心点
        shapeFactory.setCentre(new Coordinate(x, y));
        //设置长轴长度
        shapeFactory.setWidth(macroaxisDegree);
        //设置短轴长度
        shapeFactory.setHeight(brachyaxisDegree);
        //设置长轴和X轴夹角
        shapeFactory.setRotation(radians);
        //生成椭圆对象
        Polygon ellipse = shapeFactory.createEllipse();
        return ellipse;
    }

    /**
     * 根据所在纬度，将X轴的长度（米）转换成经度
     * （因为不同纬度下，1°经度代表的长度不同）
     *
     * @param y      所在纬度
     * @param length 线段长度
     * @return
     */
    public static double parseXLengthToDegree(double y, double length) {
        //将角度（纬度）转换为弧度
        double latRadian = Math.toRadians(y);
        //计算当前纬度地球周长
        double latPEarth = P_EARTH * Math.cos(latRadian);
        //将length长度转换为度数
        double xDegree = length / latPEarth * 360;
        return xDegree;
    }

    /**
     * 将Y轴的长度（米）转换成纬度
     *
     * @param length
     * @return
     */
    public static double parseYLengthToDegree(double length) {
        //将length长度转换为度数
        double yDegree = length / P_EARTH * 360;
        return yDegree;
    }
}
